package com.cat.graphTheory;

import java.util.Arrays;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/shortest-unsorted-continuous-subarray/
 * @create 2025/10/7 21:17
 * @since JDK17
 */

public class Solution40 {
    public int findUnsortedSubarray(int[] nums) {
        int n = nums.length, first = -2, last = -1;
        int[] arr = Arrays.copyOf(nums, n);
        Arrays.sort(arr);

        for (int i = 0; i < n; i++) {   //
            if (arr[i] != nums[i]) {
                if (first == -2) {
                    first = i;
                }
                last = i;
            }
        }
        return last - first + 1;
    }
}
